#=
测试抽样
=#

include("visliz_helper.jl")


function run()
    Np = 2
    Nt = 2
    vol = [2.0, 2.0, 2.0]
    fbup = random_path(Float64, Np, Nt, vol)
    fbdn = random_path(Float64, Np, Nt, vol)
    fbne = random_point(Float64, Np, Nt, vol)
    plt = plot()
    println(fbup)
    draw_fb3D!(plt, fbup, :red; hidend=false)
    draw_fb3D!(plt, fbdn, :green; hidend=false)
    draw_fb3D!(plt, fbne, :blue; hidend=false)
    savefig(plt, "test0.png")
    #
    for iter in Base.OneTo(10)
        for it in Base.OneTo(Nt-1)
            #更新之前的
            upbds1 = beads(fbup, it)
            upbds2 = beads(fbup, it+1)
            dnbds1 = beads(fbdn, it)
            dnbds2 = beads(fbdn, it+1)
            nebds1 = beads(fbne, it)
            eU, ek = spinful_rho_at(upbds1, dnbds1, upbds2, dnbds2, nebds1, vol)
            eng = eU + ek
            println("$(eU) $(ek)")
            for aip in Base.OneTo(Np)
                fbup2 = move_at(aip, it, fbup, vol)
                #println(upbds1)
                #println("eng: $(eng)")
                upbds12 = beads(fbup2, it)
                #println(upbds1)
                eU, eK = spinful_rho_at(upbds12, dnbds1, upbds2, dnbds2, nebds1, vol)
                eng2 = eU + eK
                #println("eng2: $(eng2)")
                if eng2 < eng
                    fbup = fbup2
                    eng = eng2
                end
                #
                fbdn2 = move_at(aip, it, fbdn, vol)
                dnbds12 = beads(fbdn2, it)
                eU, eK = spinful_rho_at(upbds1, dnbds12, upbds2, dnbds2, nebds1, vol)
                eng3 = eU + eK
                if eng3 < eng
                    fbdn = fbdn2
                    eng = eng3
                end
            end
            #
        end
        #最后一个Nt的更新
        upbds1 = beads(fbup, Nt)
        upbds2 = beads(fbup, 1)
        dnbds1 = beads(fbdn, Nt)
        dnbds2 = beads(fbdn, 1)
        nebds1 = beads(fbne, Nt)
        eU, eK = spinful_rho_exch(upbds1, dnbds1, upbds2, dnbds2, nebds1, vol)
        eng = eU + eK
        #
        for aip in Base.OneTo(Np)
            fbup2 = move_at(aip, Nt, fbup, vol)
            #每次都重新拿一下upbds12
            upbds12 = beads(fbup2, Nt)
            eU, eK = spinful_rho_exch(upbds12, dnbds1, upbds2, dnbds2, nebds1, vol)
            eng2 = eU + eK
            if eng2 < eng
                #修改了fbup之后，upbds12也会跟着改
                fbup = fbup2
                eng = eng2
            end
            fbdn2 = move_at(aip, Nt, fbdn, vol)
            dnbds12 = beads(fbdn2, Nt)
            eU, eK = spinful_rho_exch(upbds1, dnbds12, upbds2, dnbds2, nebds1, vol)
            eng3 = eU + eK
            if eng3 < eng
                fbdn = fbdn2
                eng = eng3
            end
        end
        #
        if mod(iter, 10) == 0
            println(eng)
            draw_fb3D!(plt, fbup, :pink; hidend=false)
            draw_fb3D!(plt, fbdn, :yellow; hidend=false)
            savefig(plt, "test$(iter).png")
        end
    end
    #更新一个需要改变两个ρ，现在还没加上。
    #所有粒子
end


run()

